package com.by.yz.activity;


import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.Register.DoctorREST;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.custom.CircleNetworkImageView;
import com.by.yz.custom.TechnologyTeamDividerItemDecoration;
import com.by.yz.entity.Doctorbean;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.LoginRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.IsNetWork;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Famousdoctorguan extends BaseActivity {
    RecyclerView famousDoctorView;
    SimpleStringDoctorListViewAdapter simpleStringDoctorListViewAdapter;
    List<Doctorbean> datas=new ArrayList<Doctorbean>();
    private String hospital;
    private String department;
    IsNetWork isNetWork;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.famous_list);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isNetWork.isNetworkAvailable(this)) {
            datas.clear();
            initData();
            initViews();
        }else{
            //无网络连接情况
            new AlertDialog.Builder(Famousdoctorguan.this)
                    .setTitle("提示!")
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setMessage("检测到您还没开启网络，请开启")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("开启",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    startActivity(new Intent(
                                            Settings.ACTION_WIRELESS_SETTINGS));// 进入无线网络配置界面
                                }
                            }).show();
        }
    }

    private void initViews() {
        famousDoctorView=(RecyclerView)findViewById(R.id.famousdoctor_listview);
        famousDoctorView.setLayoutManager(new LinearLayoutManager(famousDoctorView.getContext()));
        simpleStringDoctorListViewAdapter=new SimpleStringDoctorListViewAdapter(this,datas);
        famousDoctorView.setAdapter(simpleStringDoctorListViewAdapter);
        famousDoctorView.addItemDecoration(new TechnologyTeamDividerItemDecoration(this,TechnologyTeamDividerItemDecoration.VERTICAL_LIST));

        setToolbarTitle("医生列表");
    }

    private void initData() {
//        LoginRESTFul loginRESTFul = new LoginRESTFul();
//        loginRESTFul.login("admin","admin",new LoginRESTFul.ILoginCb(){
//            @Override
//            public void ok(String response) {
                hospital = getIntent().getStringExtra("hospital");
                department = getIntent().getStringExtra("department");
                Log.e("医院get = ",hospital);
                Log.e("科室get = ",department);
                DoctorREST doctorREST=new DoctorREST();
                doctorREST.getDoctorRESTo(new DoctorREST.MyGetCallBack() {
                    @Override
                    public void ok(JSONArray response) throws JSONException {
                        boolean flag = true;
                        Doctorbean doctorbean ;
                        for(int i=0;i<response.length();i++){
                            JSONObject jsonObject= response.getJSONObject(i);
                            doctorbean= ToJSonObject.jsonToBean(jsonObject.toString(),Doctorbean.class);
                            if (doctorbean.getFlag() && doctorbean.getTextHospital().getHospital().equals(hospital) && doctorbean.getDepartment().getDepartment().equals(department))
                            {
                                flag = false;
                                datas.add(doctorbean);
                                famousDoctorView.setVisibility(View.VISIBLE);
                            }
                        }
                        if(flag){
                            famousDoctorView.setVisibility(View.GONE);
                        }
                        simpleStringDoctorListViewAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void error(String error) {
                        Log.e("hj","更新失败"+error);
                    }
                });

                AccountRESTFul accountRESTFul= new AccountRESTFul();
                accountRESTFul.get(new AccountRESTFul.IhttpCb(){

                    @Override
                    public void ok(JSONObject response) {

                    }

                    @Override
                    public void error(String error) {

                    }
                });
    }



    public static class SimpleStringDoctorListViewAdapter
            extends RecyclerView.Adapter<SimpleStringDoctorListViewAdapter.ViewHolder> {
        private final TypedValue mTypedValue = new TypedValue();
        private int mBackground;
        private List<Doctorbean> mValues;

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public Doctorbean tt;
            public View mView;
            public final CircleNetworkImageView mImageView;
            public final TextView teamName;
            public final TextView goodAt;
            public final TextView locate;
            public final TextView instruc;
            public final TextView position;


            public ViewHolder(View view) {
                super(view);
                mView=view;
                mImageView = (CircleNetworkImageView) view.findViewById(R.id.avatar);
                teamName = (TextView) view.findViewById(R.id.famous_doctor_name_textview);
                goodAt = (TextView) view.findViewById(R.id.hospital_textview);
                locate = (TextView) view.findViewById(R.id.keshi_textview);
                instruc =(TextView) view.findViewById(R.id.shanchang_textview);
                position=(TextView)view.findViewById(R.id.touxian_textview);
            }

//            @Override
//            public String toString() {
//                return super.toString() + " '" + mTextView.getText();
//            }
        }
        public Doctorbean getValueAt(int position) {
            return mValues.get(position);
        }

        public SimpleStringDoctorListViewAdapter(Context context, List<Doctorbean> items) {
            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
            mBackground = mTypedValue.resourceId;
            mValues = items;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.famout_doctor_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder Myholder, int position) {
            Myholder.tt = mValues.get(position);
            Myholder.teamName.setText(mValues.get(position).getDoctorname());
            Myholder.goodAt.setText(mValues.get(position).getTextHospital().getHospital());
            Myholder.locate.setText(mValues.get(position).getDepartment().getDepartment());
            Myholder.position.setText(mValues.get(position).getPosition());
            Myholder.instruc.setText(mValues.get(position).getResume());
            Myholder.mImageView.setDefaultImageResId(R.mipmap.login_logo1);
            Myholder.mImageView.setErrorImageResId(R.mipmap.login_error);
            Myholder.mImageView.setImageUrl(mValues.get(position).getmImageView(), App.imageLoader);
            Myholder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, Famous_doctor_detail.class);
//                    intent.putExtra(CheeseDetailActivity.EXTRA_NAME, holder.mBoundString);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("Doctor",Myholder.tt);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });

//            Glide.with(Myholder.mImageView.getContext())
//                    .load(mValues.get(position).getmImageView())
//                    .fitCenter()
//                    .into(Myholder.mImageView);
        }

        @Override
        public int getItemCount() {
            return mValues.size();
        }


    }

}
